package javagprssrv.Listener;

import javagprssrv.Log;
import javagprssrv.StructClass.structTCPServerSocket;
import java.sql.ResultSet;
import javagprssrv.Task.TrdTaskManager;
import java.util.Vector;
import javagprssrv.DataBase.DBSQLExecute;
import java.text.DecimalFormat;
import javagprssrv.Common.CommonClass;
import DT42.DT42_DataReader;
import javagprssrv.StructClass.structIATask;
import java.util.Date;
import java.text.SimpleDateFormat;
import javagprssrv.Common.DateTime;
import javagprssrv.JGS_Param;
import java.nio.ByteBuffer;
import java.util.Map;

public class TrdJSSJDataExecutor extends java.lang.Thread {

    java.lang.String ClientIP;
    int ClientPort;
    java.lang.String CMD;
    int TimeOutSeconds;
    java.lang.String TimeOutTip;
    java.lang.String startTime;
    boolean isWaitForData;
    private java.nio.ByteBuffer buffer;

    public TrdJSSJDataExecutor(final java.lang.String _ClientIP, final int _ClientPort, final java.lang.String _CMD) {
        super("TrdJSSJDataExecutor");
        this.TimeOutSeconds = 90;
        this.TimeOutTip = "";
        this.startTime = "";
        this.isWaitForData = false;
        this.buffer = java.nio.ByteBuffer.allocate(4096);
        this.CMD = _CMD;
        this.ClientIP = _ClientIP;
        this.ClientPort = _ClientPort;
        this.TimeOutSeconds = javagprssrv.JGS_Param._TaskFailedSeconds + 20;
        this.startTime = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
    }

    @java.lang.Override
    public void run() {
        while (true) {
            int between = -1;
            try {
                final java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                final java.util.Date begin = df.parse(this.startTime);
                final java.util.Date end = new java.util.Date();
                between = java.lang.Double.valueOf((end.getTime() - begin.getTime()) / 1000L).intValue();
            } catch (java.lang.Exception ex) {
            }
            if (between > this.TimeOutSeconds) {
                this.TimeOutTip = "ERROR:在执行即时通讯时，该任务出现超时错误，现已经被系统关闭。";
                this.TimeOutTip += "可能的原因如下：";
                this.TimeOutTip += "<br>&nbsp;&nbsp;(1)仪表暂不在网络服务区内，请拨打该仪表号码进行测试；";
                this.TimeOutTip += "<br>&nbsp;&nbsp;(2)仪表或因欠费等原因无法开启GPRS服务，请核实卡内金额；";
                this.TimeOutTip += "<br>&nbsp;&nbsp;(3)仪表是否已经正常供电，请检查核实；";
                this.TimeOutTip += "<br>&nbsp;&nbsp;(4)仪表故障，请与我公司联系；";
                this.TimeOutTip += "<br>&nbsp;&nbsp;给您的工作带来不便，为此我们深表歉意。";
                this.TCPServerSocketsSendData(this.ClientIP, this.ClientPort, this.TimeOutTip);
                break;
            }
            try {
                java.lang.Thread.sleep(100L);
                if (this.isWaitForData) {
                    boolean isIATaskDone = false;
                    java.lang.String RecvData = "";
                    synchronized (javagprssrv.JGS_Param.IATasks) {
                        for (int idx = javagprssrv.JGS_Param.IATasks.size() - 1; idx >= 0; --idx) {
                            if (((structIATask) javagprssrv.JGS_Param.IATasks.elementAt(idx)).ClientIP.equals(this.ClientIP) && ((structIATask) javagprssrv.JGS_Param.IATasks.elementAt(idx)).ClientPort == this.ClientPort && ((structIATask) javagprssrv.JGS_Param.IATasks.elementAt(idx)).RecvData.length() > 0) {
                                RecvData = ((structIATask) javagprssrv.JGS_Param.IATasks.elementAt(idx)).RecvData;
                                isIATaskDone = true;
                                break;
                            }
                        }
                    }
                    if (isIATaskDone) {
                        java.lang.String RecvData_JieXi = "";
                        RecvData_JieXi = this.RecvData_JieXi(RecvData);
                        try {
                            this.TCPServerSocketsSendData(this.ClientIP, this.ClientPort, RecvData_JieXi);
                        } catch (java.lang.Exception ex2) {
                        }
                        break;
                    }
                    continue;
                } else {
                    this.isWaitForData = true;
                    this.createJSSJTask(this.CMD);
                }
            } catch (java.lang.Exception ex3) {
            }
        }
        synchronized (javagprssrv.JGS_Param.IATasks) {
            for (int idx2 = javagprssrv.JGS_Param.IATasks.size() - 1; idx2 >= 0; --idx2) {
                if (((structIATask) javagprssrv.JGS_Param.IATasks.elementAt(idx2)).ClientIP.equals(this.ClientIP) && ((structIATask) javagprssrv.JGS_Param.IATasks.elementAt(idx2)).ClientPort == this.ClientPort) {
                    javagprssrv.JGS_Param.IATasks.remove(idx2);
                }
            }
        }
        this.TCPServerSocketsRemoveByRemoteIPPort(this.ClientIP, this.ClientPort);
    }

    private java.lang.String RecvData_JieXi(final java.lang.String Data) {
        java.lang.String res = "";
        int firstChar = -1;
        firstChar = Data.charAt(0);
        if (firstChar == 65) {
            java.lang.String year = "";
            java.lang.String month = "";
            java.lang.String date = "";
            java.lang.String zyxs = "";
            java.lang.String csxs = "";
            java.lang.String cxxs = "";
            java.lang.String zdz = "";
            java.lang.String zdzs = "";
            java.lang.String zxz = "";
            java.lang.String zxzs = "";
            java.lang.String pjz = "";
            final int cmd = Data.charAt(4);
            if (cmd == 3) {
                res = "TYPE=MonthData";
            }
            if (cmd == 4 || cmd == 5) {
                res = "TYPE=DateData";
            }
            if (cmd == 3 || cmd == 4 || cmd == 5) {
                year = "20" + Data.substring(9, 11);
                month = Data.substring(11, 13);
                date = Data.substring(13, 15);
                zyxs = Data.substring(15, 20);
                csxs = Data.substring(20, 25);
                cxxs = Data.substring(25, 30);
                zdz = Data.substring(30, 34);
                zdzs = Data.substring(34, 42);
                zxz = Data.substring(42, 46);
                zxzs = Data.substring(46, 54);
                pjz = Data.substring(54, 58);
                res = res + "^YEAR=" + year + "^MONTH=" + month + "^DATE=" + date + "^ZYXS=" + zyxs + "^CSXS=" + csxs + "^CXXS=" + cxxs + "^ZDZ=" + zdz + "^ZDZS=" + zdzs + "^ZXZ=" + zxz + "^ZXZS=" + zxzs + "^PJZ=" + pjz + "^";
            }
            if (cmd == 13) {
                res = "TYPE=GetTIME^TIME=";
                res = res + Data.substring(9, 21) + "^";
            }
            if (cmd == 14) {
                res = "TYPE=GetSXX^PARAM1=";
                res = res + Data.substring(9, 17) + "^SXZ=" + Data.substring(17, 21) + "^XXZ=" + Data.substring(21, 25) + "^PARAM2=" + Data.substring(25, 61) + "^";
            }
        }
        if (firstChar == 104) {
            final DT42.DT42_DataReader DT42Reader = new DT42.DT42_DataReader();
            DT42Reader.ReadDT42Data(Data);
            int idx = 0;
            int idx2 = 0;
            java.lang.String s2_Date = "";
            java.lang.String s2_Month = "";
            java.lang.String s2_Year = "";
            if (DT42Reader.AFN == 10 && DT42Reader.Fn == 53) {
                res = "TYPE=GetTIME";
                final java.lang.String DT = DT42Reader.Data;
                res = res + "^TIME=" + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(5, 6)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(4, 5)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(3, 4)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(2, 3)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(1, 2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(0, 1)) + "^";
            }
            if (DT42Reader.AFN == 10 && DT42Reader.Fn == 34) {
                res = "TYPE=GetSXX^";
                final java.lang.String DT = DT42Reader.Data;
                final java.lang.String Param1 = "";
                final java.lang.String SXZ = "" + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(2, 3)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(1, 2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(0, 1));
                final java.lang.String XXZ = "" + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(5, 6)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(4, 5)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(3, 4));
                final java.lang.String Param2 = "" + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(6, 7)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(7, 8)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(8, 9)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(9, 10)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(10, 11)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(11, 12));
                res = res + "PARAM1=" + Param1 + "^SXZ=" + SXZ + "^XXZ=" + XXZ + "^PARAM2=" + Param2 + "^";
            }
            if (DT42Reader.AFN == 13 && DT42Reader.Fn == 33) {
                res = "TYPE=DATEDATA";
                idx = 0;
                idx2 = 0;
                s2_Date = new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(0, 1));
                s2_Month = new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(1, 2));
                s2_Year = "20" + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(2, 3));
            }
            if (DT42Reader.AFN == 13 && DT42Reader.Fn == 36) {
                res = "TYPE=MONTHData";
                idx = -1;
                idx2 = 1;
                s2_Month = new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(0, 1));
                s2_Year = "20" + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(1, 2));
            }
            if (DT42Reader.AFN == 13) {
                final int zyxs_0 = DT42Reader.Data.charAt(3 + idx);
                final int zyxs_2 = DT42Reader.Data.charAt(4 + idx);
                final java.lang.String zyxs2 = java.lang.String.valueOf(zyxs_0 + zyxs_2 * 256);
                final int gzs_0 = DT42Reader.Data.charAt(5 + idx);
                final int gzs_2 = DT42Reader.Data.charAt(6 + idx);
                final java.lang.String gzs = java.lang.String.valueOf(gzs_0 + gzs_2 * 256);
                final int csxs_0 = DT42Reader.Data.charAt(7 + idx);
                final int csxs_2 = DT42Reader.Data.charAt(8 + idx);
                final java.lang.String csxs2 = java.lang.String.valueOf(csxs_0 + csxs_2 * 256);
                final int cxxs_0 = DT42Reader.Data.charAt(9 + idx);
                final int cxxs_2 = DT42Reader.Data.charAt(10 + idx);
                final java.lang.String cxxs2 = java.lang.String.valueOf(cxxs_0 + cxxs_2 * 256);
                final int hgs_0 = DT42Reader.Data.charAt(13 + idx2);
                final int hgs_2 = DT42Reader.Data.charAt(14 + idx2);
                final java.lang.String hgs = java.lang.String.valueOf(hgs_0 + hgs_2 * 256);
                java.lang.String s_zdz = new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(17 + idx2, 18 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(16 + idx2, 17 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(15 + idx2, 16 + idx2));
                final java.lang.String zdzs2 = s2_Month + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(20 + idx2, 21 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(19 + idx2, 20 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(18 + idx2, 19 + idx2));
                java.lang.String s_zxz = new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(23 + idx2, 24 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(22 + idx2, 23 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(21 + idx2, 22 + idx2));
                final java.lang.String zxzs2 = s2_Month + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(26 + idx2, 27 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(25 + idx2, 26 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(24 + idx2, 25 + idx2));
                java.lang.String s_pjz = new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(29 + idx2, 30 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(28 + idx2, 29 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(27 + idx2, 28 + idx2));
                s_zdz = s_zdz.substring(0, 3) + "." + s_zdz.substring(3, 5);
                s_zxz = s_zxz.substring(0, 3) + "." + s_zxz.substring(3, 5);
                s_pjz = s_pjz.substring(0, 3) + "." + s_pjz.substring(3, 5);
                final double YB_BL = 1.0;
                final java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
                final double d_zdz = java.lang.Double.valueOf(s_zdz);
                s_zdz = df.format(d_zdz * YB_BL);
                final double d_zxz = java.lang.Double.valueOf(s_zxz);
                s_zxz = df.format(d_zxz * YB_BL);
                final double d_pjz = java.lang.Double.valueOf(s_pjz);
                s_pjz = df.format(d_pjz * YB_BL);
                res = res + "^YEAR=" + s2_Year + "^Month=" + s2_Month + "^Date=" + s2_Date + "^ZYXS=" + zyxs2 + "^CSXS=" + csxs2 + "^CXXS=" + cxxs2 + "^ZDZ=" + s_zdz + "^ZDZS=" + zdzs2 + "^ZXZ=" + s_zxz + "^ZXZS=" + zxzs2 + "^PJZ=" + s_pjz + "^";
            }
        }
        return res;
    }

    private java.lang.String RecvData_JieXi_BAK(final java.lang.String Data) {
        java.lang.String res = "";
        int firstChar = -1;
        firstChar = Data.charAt(0);
        if (firstChar == 65) {
            java.lang.String year = "";
            java.lang.String month = "";
            java.lang.String date = "";
            java.lang.String zyxs = "";
            java.lang.String csxs = "";
            java.lang.String cxxs = "";
            java.lang.String zdz = "";
            java.lang.String zdzs = "";
            java.lang.String zxz = "";
            java.lang.String zxzs = "";
            java.lang.String pjz = "";
            final int cmd = Data.charAt(4);
            if (cmd == 3) {
                res = "种类=月数据";
            }
            if (cmd == 4 || cmd == 5) {
                res = "种类=日数据";
            }
            if (cmd == 3 || cmd == 4 || cmd == 5) {
                year = "20" + Data.substring(9, 11);
                month = Data.substring(11, 13);
                date = Data.substring(13, 15);
                zyxs = Data.substring(15, 20);
                csxs = Data.substring(20, 25);
                cxxs = Data.substring(25, 30);
                zdz = Data.substring(30, 34);
                zdzs = Data.substring(34, 42);
                zxz = Data.substring(42, 46);
                zxzs = Data.substring(46, 54);
                pjz = Data.substring(54, 58);
                res = res + "^年=" + year + "^月=" + month + "^日=" + date + "^总运行时=" + zyxs + "^超上限时=" + csxs + "^超下限时=" + cxxs + "^最大值=" + zdz + "^最大值时=" + zdzs + "^最小值=" + zxz + "^最小值时=" + zxzs + "^平均值=" + pjz + "^";
            }
            if (cmd == 13) {
                res = "种类=获取仪表时钟^当前时间=";
                res = res + Data.substring(9, 21) + "^";
            }
            if (cmd == 14) {
                res = "种类=获取仪表上下限^PARAM1=";
                res = res + Data.substring(9, 17) + "^上限值=" + Data.substring(17, 21) + "^下限值=" + Data.substring(21, 25) + "^PARAM2=" + Data.substring(25, 61) + "^";
            }
        }
        if (firstChar == 104) {
            final DT42.DT42_DataReader DT42Reader = new DT42.DT42_DataReader();
            DT42Reader.ReadDT42Data(Data);
            int idx = 0;
            int idx2 = 0;
            java.lang.String s2_Date = "";
            java.lang.String s2_Month = "";
            java.lang.String s2_Year = "";
            if (DT42Reader.AFN == 10 && DT42Reader.Fn == 53) {
                res = "种类=获取仪表时钟";
                final java.lang.String DT = DT42Reader.Data;
                res = res + "^当前时间=" + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(5, 6)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(4, 5)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(3, 4)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(2, 3)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(1, 2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(0, 1)) + "^";
            }
            if (DT42Reader.AFN == 10 && DT42Reader.Fn == 34) {
                res = "种类=获取仪表上下限^";
                final java.lang.String DT = DT42Reader.Data;
                final java.lang.String Param1 = "";
                final java.lang.String SXZ = "" + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(2, 3)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(1, 2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(0, 1));
                final java.lang.String XXZ = "" + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(5, 6)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(4, 5)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(3, 4));
                final java.lang.String Param2 = "" + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(6, 7)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(7, 8)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(8, 9)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(9, 10)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(10, 11)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT.substring(11, 12));
                res = res + "PARAM1=" + Param1 + "^上限值=" + SXZ + "^下限值=" + XXZ + "^PARAM2=" + Param2 + "^";
            }
            if (DT42Reader.AFN == 13 && DT42Reader.Fn == 33) {
                res = "种类=日数据";
                idx = 0;
                idx2 = 0;
                s2_Date = new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(0, 1));
                s2_Month = new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(1, 2));
                s2_Year = "20" + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(2, 3));
            }
            if (DT42Reader.AFN == 13 && DT42Reader.Fn == 36) {
                res = "种类=月数据";
                idx = -1;
                idx2 = 1;
                s2_Month = new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(0, 1));
                s2_Year = "20" + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(1, 2));
            }
            if (DT42Reader.AFN == 13) {
                final int zyxs_0 = DT42Reader.Data.charAt(3 + idx);
                final int zyxs_2 = DT42Reader.Data.charAt(4 + idx);
                final java.lang.String zyxs2 = java.lang.String.valueOf(zyxs_0 + zyxs_2 * 256);
                final int gzs_0 = DT42Reader.Data.charAt(5 + idx);
                final int gzs_2 = DT42Reader.Data.charAt(6 + idx);
                final java.lang.String gzs = java.lang.String.valueOf(gzs_0 + gzs_2 * 256);
                final int csxs_0 = DT42Reader.Data.charAt(7 + idx);
                final int csxs_2 = DT42Reader.Data.charAt(8 + idx);
                final java.lang.String csxs2 = java.lang.String.valueOf(csxs_0 + csxs_2 * 256);
                final int cxxs_0 = DT42Reader.Data.charAt(9 + idx);
                final int cxxs_2 = DT42Reader.Data.charAt(10 + idx);
                final java.lang.String cxxs2 = java.lang.String.valueOf(cxxs_0 + cxxs_2 * 256);
                final int hgs_0 = DT42Reader.Data.charAt(13 + idx2);
                final int hgs_2 = DT42Reader.Data.charAt(14 + idx2);
                final java.lang.String hgs = java.lang.String.valueOf(hgs_0 + hgs_2 * 256);
                java.lang.String s_zdz = new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(17 + idx2, 18 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(16 + idx2, 17 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(15 + idx2, 16 + idx2));
                final java.lang.String zdzs2 = s2_Month + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(20 + idx2, 21 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(19 + idx2, 20 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(18 + idx2, 19 + idx2));
                java.lang.String s_zxz = new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(23 + idx2, 24 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(22 + idx2, 23 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(21 + idx2, 22 + idx2));
                final java.lang.String zxzs2 = s2_Month + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(26 + idx2, 27 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(25 + idx2, 26 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(24 + idx2, 25 + idx2));
                java.lang.String s_pjz = new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(29 + idx2, 30 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(28 + idx2, 29 + idx2)) + new javagprssrv.Common.CommonClass()._BCD2STR(DT42Reader.Data.substring(27 + idx2, 28 + idx2));
                s_zdz = s_zdz.substring(0, 3) + "." + s_zdz.substring(3, 5);
                s_zxz = s_zxz.substring(0, 3) + "." + s_zxz.substring(3, 5);
                s_pjz = s_pjz.substring(0, 3) + "." + s_pjz.substring(3, 5);
                final double YB_BL = 1.0;
                final java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
                final double d_zdz = java.lang.Double.valueOf(s_zdz);
                s_zdz = df.format(d_zdz * YB_BL);
                final double d_zxz = java.lang.Double.valueOf(s_zxz);
                s_zxz = df.format(d_zxz * YB_BL);
                final double d_pjz = java.lang.Double.valueOf(s_pjz);
                s_pjz = df.format(d_pjz * YB_BL);
                res = res + "^年=" + s2_Year + "^月=" + s2_Month + "^日=" + s2_Date + "^总运行时=" + zyxs2 + "^超上限时=" + csxs2 + "^超下限时=" + cxxs2 + "^最大值=" + s_zdz + "^最大值时=" + zdzs2 + "^最小值=" + s_zxz + "^最小值时=" + zxzs2 + "^平均值=" + s_pjz + "^";
            }
        }
        return res;
    }

    private void createJSSJTask(java.lang.String strCMD) {
        java.lang.String DeviceID = "";
        java.lang.String DeviceModel = "";
        final java.lang.String DeviceTZString = "DeviceID";
        int pos_dengyu = -1;
        int pos_shangjiantou = -1;
        pos_dengyu = strCMD.indexOf("=");
        pos_shangjiantou = strCMD.indexOf("^");
        DeviceID = strCMD.substring(pos_dengyu + 1, pos_shangjiantou);
        final javagprssrv.DataBase.DBSQLExecute dbse_Device_Model = new javagprssrv.DataBase.DBSQLExecute();

        try {

            Map<String, Object> map = dbse_Device_Model.findModel(DeviceID);
            if (map != null) {
                DeviceModel = (String) map.get("model");
            }
        } catch (java.lang.Exception ex) {
        }
        if (DeviceModel.equals("DT4-2")) {
            java.lang.String A_A1 = "";
            java.lang.String A_A2 = "";
            java.lang.String LuShu = "";
            java.lang.String TelNO = "";
            java.lang.String TermNO = "";
            java.lang.String CMD = "";
            final java.util.Vector CMDs = new java.util.Vector();
            java.lang.String ASYNTelNO = "";
            final java.lang.String RemoteIP = "";
            final boolean CanBeASYN = false;
            for (int i = strCMD.indexOf("^"); i >= 0; i = strCMD.indexOf("^")) {
                final java.lang.String s = strCMD.substring(0, i);
                final int j = s.indexOf("=");
                final java.lang.String strName = s.substring(0, j);
                final java.lang.String strValue = s.substring(j + 1);
                if (strName.toUpperCase().equals("DEVICEID")) {
                    DeviceID = strValue;
                }
                if (strName.toUpperCase().equals("CMD")) {
                    CMD = strValue;
                }
                strCMD = strCMD.substring(i + 1);
            }
            try {
                final java.lang.String sql = "select * from t_Device where ID=" + DeviceID;
                final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
                Map<String, Object> map = dbse.findYbInfo2(DeviceID);
                if (map != null) {
                    A_A1 = (String) map.get("adminAreaCode");
                    A_A2 = (String) map.get("terminalNO");
                    LuShu = (String) map.get("channel");
                }
                final java.lang.String _CMD_20 = CMD + "          ";
                if (_CMD_20.substring(0, 8).toUpperCase().equals("GET DATE")) {
                    CMD = CMD.substring(0, 8) + LuShu + CMD.substring(8);
                }
                if (_CMD_20.substring(0, 9).toUpperCase().equals("GET MONTH")) {
                    CMD = CMD.substring(0, 9) + LuShu + CMD.substring(9);
                }
                if (_CMD_20.substring(0, 7).toUpperCase().equals("GET SXX")) {
                    CMD = CMD.substring(0, 7) + LuShu;
                }
                if (_CMD_20.substring(0, 7).toUpperCase().equals("SET SXX")) {
                    CMD = CMD.substring(0, 7) + LuShu + CMD.substring(7);
                }
                TelNO = A_A1 + A_A2;
                TermNO = A_A2;
                ASYNTelNO = A_A1 + "." + A_A2;
            } catch (java.lang.Exception ex2) {
            }
            synchronized (javagprssrv.JGS_Param.IATasks) {
                for (int idx = javagprssrv.JGS_Param.IATasks.size() - 1; idx >= 0; --idx) {
                    if (((structIATask) javagprssrv.JGS_Param.IATasks.elementAt(idx)).ClientIP.equals(this.ClientIP) && ((structIATask) javagprssrv.JGS_Param.IATasks.elementAt(idx)).ClientPort == this.ClientPort) {
                        javagprssrv.JGS_Param.IATasks.remove(idx);
                    }
                }
                final javagprssrv.StructClass.structIATask iaTask = new javagprssrv.StructClass.structIATask();
                iaTask.ClientIP = this.ClientIP;
                iaTask.ClientPort = this.ClientPort;
                iaTask.CMD = CMD;
                iaTask.RecvData = "";
                javagprssrv.JGS_Param.IATasks.add(iaTask);
            }
            CMDs.add(CMD);
            final java.lang.String time = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
            javagprssrv.Task.TrdTaskManager._AddTask("IADATA@" + this.ClientIP + ":" + java.lang.String.valueOf(this.ClientPort), TelNO, TermNO, CMDs, CanBeASYN, ASYNTelNO, time);
        }
        if (DeviceModel.equals("DT3") || DeviceModel.equals("DT3-GDIP")) {
            java.lang.String TelNO2 = "";
            java.lang.String TermNO2 = "";
            java.lang.String CMD2 = "";
            final java.util.Vector CMDs2 = new java.util.Vector();
            java.lang.String ASYNTelNO2 = "";
            final java.lang.String RemoteIP2 = "";
            boolean CanBeASYN2 = false;
            try {
                //final java.lang.String sql2 = "select * from t_Device where ID=" + DeviceID;
                final javagprssrv.DataBase.DBSQLExecute dbse2 = new javagprssrv.DataBase.DBSQLExecute();
                Map<String, Object> map = dbse2.findYbInfo2(DeviceID);
                if (map != null) {
                    TelNO2 = (String) map.get("mobileNo");
                    TermNO2 = (String) map.get("terminalNo");
                    ASYNTelNO2 = (String) map.get("uniqueCode");
                    /*TelNO2 = rs2.getString("MobileNo").trim();
                     TermNO2 = rs2.getString("TerminalNo").trim();
                     ASYNTelNO2 = rs2.getString("uniqueCode").trim();*/
                }
                CanBeASYN2 = true;
            } catch (java.lang.Exception ex3) {
            }
            for (int k = strCMD.indexOf("^"); k >= 0; k = strCMD.indexOf("^")) {
                final java.lang.String s2 = strCMD.substring(0, k);
                final int l = s2.indexOf("=");
                final java.lang.String strName2 = s2.substring(0, l);
                final java.lang.String strValue2 = s2.substring(l + 1);
                if (strName2.toUpperCase().equals("CMD")) {
                    CMD2 = strValue2;
                }
                strCMD = strCMD.substring(k + 1);
            }
            synchronized (javagprssrv.JGS_Param.IATasks) {
                for (int idx2 = javagprssrv.JGS_Param.IATasks.size() - 1; idx2 >= 0; --idx2) {
                    if (((structIATask) javagprssrv.JGS_Param.IATasks.elementAt(idx2)).ClientIP.equals(this.ClientIP) && ((structIATask) javagprssrv.JGS_Param.IATasks.elementAt(idx2)).ClientPort == this.ClientPort) {
                        javagprssrv.JGS_Param.IATasks.remove(idx2);
                    }
                }
                final javagprssrv.StructClass.structIATask iaTask2 = new javagprssrv.StructClass.structIATask();
                iaTask2.ClientIP = this.ClientIP;
                iaTask2.ClientPort = this.ClientPort;
                iaTask2.CMD = CMD2;
                iaTask2.RecvData = "";
                javagprssrv.JGS_Param.IATasks.add(iaTask2);
            }
            CMDs2.add(CMD2);
            javagprssrv.Task.TrdTaskManager._AddTask("IADATA@" + this.ClientIP + ":" + java.lang.String.valueOf(this.ClientPort), TelNO2, TermNO2, CMDs2, CanBeASYN2, ASYNTelNO2, "2010-01-01 12:34:56");
        }
    }

    private void createJSSJTask_old(java.lang.String strCMD) {
        final java.lang.String DeviceTZString = "DeviceID";
        if (strCMD.toUpperCase().indexOf(DeviceTZString.toUpperCase()) >= 0) {
            java.lang.String DeviceID = "";
            java.lang.String A_A1 = "";
            java.lang.String A_A2 = "";
            java.lang.String LuShu = "";
            java.lang.String TelNO = "";
            java.lang.String TermNO = "";
            java.lang.String CMD = "";
            final java.util.Vector CMDs = new java.util.Vector();
            java.lang.String ASYNTelNO = "";
            final java.lang.String RemoteIP = "";
            final boolean CanBeASYN = false;
            for (int i = strCMD.indexOf("^"); i >= 0; i = strCMD.indexOf("^")) {
                final java.lang.String s = strCMD.substring(0, i);
                final int j = s.indexOf("=");
                final java.lang.String strName = s.substring(0, j);
                final java.lang.String strValue = s.substring(j + 1);
                if (strName.toUpperCase().equals("DEVICEID")) {
                    DeviceID = strValue;
                }
                if (strName.toUpperCase().equals("CMD")) {
                    CMD = strValue;
                }
                strCMD = strCMD.substring(i + 1);
            }
            try {
                //final java.lang.String sql = "select * from t_Device where ID=" + DeviceID;
               
                final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
                Map<String, Object> map = dbse.findYbInfo2(DeviceID);
                if (map != null) {
                    A_A1 = (String) map.get("adminAreaCode");
                    A_A2 = (String) map.get("terminalNO");
                    LuShu = (String) map.get("channel");
                }
                
                if (CMD.substring(0, 8).toUpperCase().equals("GET DATE")) {
                    CMD = CMD.substring(0, 8) + LuShu + CMD.substring(8);
                }
                if (CMD.substring(0, 9).toUpperCase().equals("GET MONTH")) {
                    CMD = CMD.substring(0, 9) + LuShu + CMD.substring(9);
                }
                TelNO = A_A1 + A_A2;
                TermNO = A_A2;
                ASYNTelNO = A_A1 + "." + A_A2;
            } catch (java.lang.Exception ex) {
            }
            synchronized (javagprssrv.JGS_Param.IATasks) {
                for (int idx = javagprssrv.JGS_Param.IATasks.size() - 1; idx >= 0; --idx) {
                    if (((structIATask) javagprssrv.JGS_Param.IATasks.elementAt(idx)).ClientIP.equals(this.ClientIP) && ((structIATask) javagprssrv.JGS_Param.IATasks.elementAt(idx)).ClientPort == this.ClientPort) {
                        javagprssrv.JGS_Param.IATasks.remove(idx);
                    }
                }
                final javagprssrv.StructClass.structIATask iaTask = new javagprssrv.StructClass.structIATask();
                iaTask.ClientIP = this.ClientIP;
                iaTask.ClientPort = this.ClientPort;
                iaTask.CMD = CMD;
                iaTask.RecvData = "";
                javagprssrv.JGS_Param.IATasks.add(iaTask);
            }
            CMDs.add(CMD);
            final java.lang.String time = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
            javagprssrv.Task.TrdTaskManager._AddTask("IADATA@" + this.ClientIP + ":" + java.lang.String.valueOf(this.ClientPort), TelNO, TermNO, CMDs, CanBeASYN, ASYNTelNO, time);
        } else {
            java.lang.String TelNO2 = "";
            java.lang.String TermNO2 = "";
            java.lang.String CMD2 = "";
            final java.util.Vector CMDs2 = new java.util.Vector();
            java.lang.String ASYNTelNO2 = "";
            java.lang.String RemoteIP2 = "";
            boolean CanBeASYN2 = false;
            for (int k = strCMD.indexOf("^"); k >= 0; k = strCMD.indexOf("^")) {
                final java.lang.String s2 = strCMD.substring(0, k);
                final int l = s2.indexOf("=");
                final java.lang.String strName2 = s2.substring(0, l);
                final java.lang.String strValue2 = s2.substring(l + 1);
                if (strName2.toUpperCase().equals("TELNO")) {
                    TelNO2 = strValue2;
                }
                if (strName2.toUpperCase().equals("TERMNO")) {
                    TermNO2 = strValue2;
                }
                if (strName2.toUpperCase().equals("CMD")) {
                    CMD2 = strValue2;
                }
                if (strName2.toUpperCase().equals("ASYNTELNO")) {
                    ASYNTelNO2 = strValue2;
                }
                if (strName2.toUpperCase().equals("REMOTEIP")) {
                    RemoteIP2 = strValue2;
                }
                if (strName2.toUpperCase().equals("CANBEASYN")) {
                    CanBeASYN2 = strValue2.equals("1");
                }
                strCMD = strCMD.substring(k + 1);
            }
            synchronized (javagprssrv.JGS_Param.IATasks) {
                for (int idx2 = javagprssrv.JGS_Param.IATasks.size() - 1; idx2 >= 0; --idx2) {
                    if (((structIATask) javagprssrv.JGS_Param.IATasks.elementAt(idx2)).ClientIP.equals(this.ClientIP) && ((structIATask) javagprssrv.JGS_Param.IATasks.elementAt(idx2)).ClientPort == this.ClientPort) {
                        javagprssrv.JGS_Param.IATasks.remove(idx2);
                    }
                }
                final javagprssrv.StructClass.structIATask iaTask2 = new javagprssrv.StructClass.structIATask();
                iaTask2.ClientIP = this.ClientIP;
                iaTask2.ClientPort = this.ClientPort;
                iaTask2.CMD = CMD2;
                iaTask2.RecvData = "";
                javagprssrv.JGS_Param.IATasks.add(iaTask2);
            }
            CMDs2.add(CMD2);
            javagprssrv.Task.TrdTaskManager._AddTask("IADATA@" + this.ClientIP + ":" + java.lang.String.valueOf(this.ClientPort), TelNO2, TermNO2, CMDs2, CanBeASYN2, ASYNTelNO2, "2010-01-01 12:34:56");
        }
    }

    public synchronized void TCPServerSocketsSendData(final java.lang.String RemoteIP, final int RemotePort, final java.lang.String Data) {
        for (int i = 0; i < javagprssrv.JGS_Param.JSSJClients.size(); ++i) {
            final java.lang.String ip = ((structTCPServerSocket) javagprssrv.JGS_Param.JSSJClients.elementAt(i)).RemoteIP;
            final int port = ((structTCPServerSocket) javagprssrv.JGS_Param.JSSJClients.elementAt(i)).RemotePort;
            if (RemoteIP.equals(ip) && RemotePort == port) {
                final javagprssrv.StructClass.structTCPServerSocket ss = (structTCPServerSocket) javagprssrv.JGS_Param.JSSJClients.elementAt(i);
                this.buffer = java.nio.ByteBuffer.wrap(Data.getBytes());
                java.lang.String s = "[P=TCP_JSSJ,T=SEND " + java.lang.Integer.toString(Data.length()) + ",D=";
                s = s + ip + ":";
                s = s + java.lang.Integer.valueOf(port).toString().trim() + "] ";
                s += new javagprssrv.Log().String2HexString(Data, " ");
                try {
                    ss.ServerChannel.write(this.buffer);
                    javagprssrv.JGS_Param.myLog.ShowLog(s, java.lang.Boolean.TRUE, java.lang.Boolean.TRUE);
                } catch (java.lang.Exception e) {
                    javagprssrv.JGS_Param.myLog.ShowLog(" [传输发生异常 : " + e.toString() + "] " + s, java.lang.Boolean.TRUE, java.lang.Boolean.TRUE);
                }
            }
        }
    }

    public synchronized void TCPServerSocketsRemoveByRemoteIPPort(final java.lang.String RemoteIP, final int RemotePort) {
        for (int i = 0; i < javagprssrv.JGS_Param.JSSJClients.size(); ++i) {
            final java.lang.String ip = ((structTCPServerSocket) javagprssrv.JGS_Param.JSSJClients.elementAt(i)).RemoteIP;
            final int port = ((structTCPServerSocket) javagprssrv.JGS_Param.JSSJClients.elementAt(i)).RemotePort;
            if (RemoteIP.equals(ip) && RemotePort == port) {
                try {
                    ((structTCPServerSocket) javagprssrv.JGS_Param.JSSJClients.elementAt(i)).ServerChannel.close();
                    javagprssrv.JGS_Param.JSSJClients.remove(i);
                } catch (java.lang.Exception ex) {
                }
            }
        }
    }
}
